-- ActivityBox
-- Created by chendh Oct/24/2016
-- 活动副本储物箱格子

-- 初始化
function initActivityBox(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 音效
            AudioM.playFx("equip_unload");

            -- 更新建筑访问
            self:updateBuildingVisit();

            local canTakeBonus = true;
            -- 遍历所有的格子，如果有非观众的怪物还或者，则不能领取奖励
            local grids = DungeonM.getCurrentDungeon();

            for _, grid in pairs(grids) do
                -- 还有怪物没死
                if grid:isMonster() and
                    not grid.monster:isAudience() and
                    not grid.monster:isDead() then
                    canTakeBonus = false;
                    break;
                end
            end

            if canTakeBonus then
                local pos = self.gridData:getPos();

                -- 播放开箱特效
                if not self.gridData.item_display then
                    self.item:removeAllChildren();
                    self.icon = nil;
                    openBoxEffect1(self.item, "darkgoldenrod", 0, 0);
                end

                -- 执行建筑奖励
                DungeonActionM.go("do_building_bonus", pos);
            else
                -- 如果有配置，则使用配置的提示语，否则使用通用的失败提示
                if string.len(buildingInfo["fail_tip"]) > 0 then
                    alert(buildingInfo["fail_tip"]);
                else
                    alert(getLocStr("pick_up_instance_target_fail"));
                end
            end
        end
    end

    local iconIndex = buildingInfo["icon"];
    local iconPath = getLevelItemIconPath(iconIndex);

    if self.gridData.item_display then
        -- 使用道具图标
        local itemId = self.gridData.item_display;
        local fileName = ItemM.query(itemId, "icon");
        iconPath = getItemIconPath(fileName);
        self.hasShadow = true;

        -- 物品根据配置是否闪光
        if ItemM.query(itemId, "shinning") == 1
            or self.gridData.need_shining == 1 then
            self.needShining = true;
        end
    end

    self.icon = ccui.ImageView:create(iconPath);

    if self.hasShadow then
        self.icon:setPositionY(5);
        if self:isKeyGrid() then
            self.icon:setPositionY(0);
        end

        if not self.hideShadow then
            self.itemShadow = ccui.ImageView:create(getLevelItemIconPath("shadow"));
            self.itemShadow:setPositionY(10);
            self.item:addChild(self.itemShadow);
        end

        self:itemAppear();
    end

    if self.needShining then
        self.shiningAjustPos = cc.p(70, 100);
        shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);
    end

    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
end
